This page last changed on Jul 18, 2006 by aperepel.

This is a simple example of how a remote web-service can be executed through SOAP. It is useful when the web-service is hosted on a server with an unavailable MULE provider such as a .NET web-service.
WebServiceX.NET is a site which hosts web-services written using the .NET platform. We are going to use a web-service available here which is able to carry out currency conversion. All details about this service can be found here.
To sum-up, this remote service requires 2 string parameters, a FromCurrency and a ToCurrency parameter which both of them are string values. Furthermore, the values of these two parameters must be one from the supplied list found on the given link. If the web-service is called with a parameter outside of this list, an exception is thrown (so be careful while trying your own examples with this web-service).

As usual, most of the magic in MULE occurs in the configuration file, and again, in this example, it turns out to be a very simple configuration. The first thing that must be noted is that if we have a web-service which requires more than one parameter, then we must send our parameters in an array of Object (Object[]). So what we are going to do first is decide how we are going to let the user enter the to and from currencies. USD GBP is good enough, so the user is asked to enter the currencies separated by a white space. At this point, we will implement a simple transformer which will get USD GBP as a string and convert it into an array of Object. The code of the mentioned transformer follows.

public class StringToObjectArray extends AbstractTransformer
{

    private static final long serialVersionUID = 18529574565544L;

    public StringToObjectArray()
    {
        super();
        this.registerSourceType(String.class);
    }


    public Object doTransform(Object src, String encoding) throws TransformerException
    {
    	String input = (String)src;
    	return input.split(" ");
    }
}

We have the transformer ready, so now let us look at the configuration file. In a few words, we need to do the following:
1.Look for a string on a channel (the to and from conversion currencies), let us use a VM channel
2.Apply the transformer to this channel to obtain an array of Object
3.Send the SOAP request
4.Return the result back on the original channel (since we are using synchronous calls)

<model name="soapDotNetSample">
        <mule-descriptor name="serviceProxy" implementation="org.mule.components.simple.BridgeComponent">
            <inbound-router>
                <endpoint address="vm://webService" transformers="StringToObjectArray"/>
            </inbound-router>
            <outbound-router>
                <router className="org.mule.routing.outbound.OutboundPassThroughRouter">
                    <endpoint address="axis:http://www.webservicex.net/CurrencyConvertor.asmx?method=ConversionRate">
                        <properties>
                            <property name="soapAction" value="${methodNamespace}${method}"/>
                            <map name="soapMethods">
                                <list name="qname{ConversionRate:http://www.webserviceX.NET/}">
                                    <entry value="FromCurrency;string;in"/>
				    <entry value="ToCurrency;string;in"/>
                                    <entry value="ConversionRateResult;double;out"/>
                                </list>
                            </map>
                        </properties>
                    </endpoint>
                </router>
            </outbound-router>
        </mule-descriptor>
</model>

The above is all the configuration we really need. So let us explain bit by bit. The inbound router as explained in the requirements is set to listen on a vm endpoint. Once a message has been received, the transformer StringToObjectArray is applied which is the transformer explained above. This result is then handed over to the BridgeComponent, which its job is exactly that of doing nothing! Therefore the exactly same message is forwarded over to the outbound router, which turns out to be our web-service method.

If we dig into the WSDL, we can figure out that the method we need to call is named ConversionRate(), which explains the

method=ConversionRate

parameter in the endpoint address. We have to set some other properties for the SOAP message to be understood perfectly by the web-service. One of them is the list of parameters and return results. This is done in the

<map name="soapMethods">

. In this example, we have 2 parameters and 1 return result.

<entry value="FromCurrency;string;in"/>

writes in the soap message that the FromCurrency is a string parameter and it is an input parameter.

<entry value="ToCurrency;string;in"/>

does exactly the same work but on the ToCurrency parameter.

<entry value="ConversionRateResult;double;out"/>

says that ConversionRateResult is the return result of the web-service and its type is of double. You have to be careful at this point because most of the time this result will be wrapped in a Double object and will not be returned as a simple double value-type.
At this point, we define no extra routers and the result will automatically get routed back to the original caller. One thing that must be noted is that this method returns one simple result. If we called a method that returns multiple results, an XSLT stylesheet can be written to convert the returned result into an object using the XmlToObject transformer.

Exception Handling

TODO

Document generated by Confluence on Nov 27, 2006 10:27